// pages/shulin/shulin.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    player:{},
    enemy:{},
    tlist:[],
    text:'要做什么',
    showhp:false,
    maxhp:null,
    width:320,
    showTansuo:true,
    showZhandou:false,
    showzdDialog:false,
    showBar:false,
    showUse:false,
    count:0

  },
  tansuo:function(){
    var that = this;
    if(that.data.count>6){
      that.setData({
        text:'今天已经探索很久了，回营地吧',
        showTansuo:false,
        showZhandou:false,
        showzdDialog:false,
        showBar:false,
        dead:true
      })
    }else{
    wx.request({
      url: 'http://localhost:8080/tansuo',
      data:{

      },
      header:{
        "content-type":"application/json"
      },
      success:function(res){
        console.log(res.data);
        if(res.data=="敌人出现了！"){
          
        
    wx.request({
      url: 'http://localhost:8080/enemy',
      data:{

      },
      header:{
        "content-type":"application/json"
      },
      success:function(res){
        console.log(res.data);
        that.setData({
          enemy:res.data
        });
        var name = that.data.enemy.name;
        var maxhp = that.data.enemy.maxhp;
        
        that.setData({
          
          text:name+"出现了！",
          showhp:true,
          maxhp:maxhp,
          width:320,
          showTansuo:false,
          showZhandou:true
        });
      }
    });
   
        }else{
          if(res.data=="获得了树枝。要做什么"){
            app.globalData.things.push('树枝');
            console.log(app.globalData.things);
          }
          if(res.data=="获得了石头。要做什么"){
            app.globalData.things.push('石头');
            console.log(app.globalData.things);
          }
          if(res.data=="获得了破布。要做什么"){
            app.globalData.things.push('破布');
            console.log(app.globalData.things);
          }
        that.setData({
          text:res.data
        });
      }
      }
    });
  }
    (this.data.count)++; 
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  taopao:function(){
    this.setData({
      text:'逃走了。。',
      enemy:{},
      showhp:false,
      showZhandou:false,
      showTansuo:true,
      showzdDialog:false
    })

  },
  zhandou:function(){
    console.log(app.globalData.player);
    function copy(obj){           //js对象的深拷贝
      if(typeof obj != 'object'){
        return obj;
      }
      var newonj = {};
      for(var attr in obj){
        newonj[attr] = copy(obj[attr]);
      }
      return newonj;
    }
    var player = copy(app.globalData.player);
    this.setData({
      player:player,
      text:'',
      showzdDialog:true,
      showBar:true
    });

  },
  attack:function(){
    if(this.data.flag==true){
      var maxhp = this.data.maxhp;
   
    var p_harm = this.data.player.atk - this.data.enemy.def;
    var e_hp = this.data.enemy.maxhp - p_harm;
    this.data.enemy.maxhp = e_hp;
    var width = e_hp/maxhp*320;
    
    this.setData({
      text:'对敌人造成了'+p_harm+'点伤害，敌人剩余'+e_hp+'点血',
      width:width,
      showBar:false
    });
    var that = this;
    var time1 = setTimeout(function(){
     
      that.setData({
        text:'敌人无法行动'
      });
    },1500);
    var time2 = setTimeout(function(){
      that.setData({
        text:'',
        flag:false,
        showBar:true
      })

    },3000);
  if(this.data.enemy.maxhp<=0){
    clearTimeout(time1);
    clearTimeout(time2);
    var money = Math.floor((Math.random()*30)+10);
    app.globalData.player.money = app.globalData.player.money + money;
    console.log(app.globalData.player.money);
      this.setData({
        
        text:'对敌人造成了'+p_harm+'点伤害 \n'+'敌人被打败了！获得了'+money+'钱',
        enemy:{},
        showhp:false,
        showzdDialog:false,
        showZhandou:false,
        showTansuo:true
      }) ;
   
  }
  if(this.data.player.maxhp<=0){
    clearTimeout(time1);
    clearTimeout(time2);
    
   
      this.setData({
        
        text:'你被打败了',
        showhp:false,
        showzdDialog:false,
        showZhandou:false,
        dead:true
      }) 
  }

    }else{
    var maxhp = this.data.maxhp;
   
    var p_harm = this.data.player.atk - this.data.enemy.def;
    var e_hp = this.data.enemy.maxhp - p_harm;
    this.data.enemy.maxhp = e_hp;
    var width = e_hp/maxhp*320;

    var e_harm = this.data.enemy.atk - this.data.player.def;
    var p_hp = this.data.player.maxhp - e_harm;
    this.data.player.maxhp = p_hp;
    
    this.setData({
      text:'对敌人造成了'+p_harm+'点伤害，敌人剩余'+e_hp+'点血',
      width:width,
      showBar:false
    });
    var that = this;
    var time1 = setTimeout(function(){
     
      that.setData({
        text:'敌人使用了'+that.data.enemy.skill+', 对自己造成了'+e_harm+'点伤害，剩余'+p_hp+'点血'
      });
    },1500);
    var time2 = setTimeout(function(){
      that.setData({
        text:'',
        showBar:true
      })

    },3000);
  if(this.data.enemy.maxhp<=0){
    clearTimeout(time1);
    clearTimeout(time2);
    var money = Math.floor((Math.random()*30)+10);
    app.globalData.player.money = app.globalData.player.money + money;
    console.log(app.globalData.player.money);
      this.setData({
        
        text:'对敌人造成了'+p_harm+'点伤害 \n'+'敌人被打败了！获得了'+money+'钱',
        enemy:{},
        showhp:false,
        showzdDialog:false,
        showZhandou:false,
        showTansuo:true
      }) ;
   
  }
  if(this.data.player.maxhp<=0){
    clearTimeout(time1);
    clearTimeout(time2);
    
   
      this.setData({
        
        text:'你被打败了',
        showhp:false,
        showzdDialog:false,
        showZhandou:false,
        dead:true
      }) 
  }
    }
  },
  defend:function(){
    var time3,time4;
    this.setData({
      text:'你全力防御了',
      showBar:false
    });
    var that = this;
    var e_harm = this.data.enemy.atk - this.data.player.def - 10;
    var p_hp = this.data.player.maxhp - e_harm;
    this.data.player.maxhp = p_hp;
    clearTimeout(time3);
    clearTimeout(time4);
      time3 = setTimeout(function(){
     
      that.setData({
        text:'敌人使用了'+that.data.enemy.skill+', 对自己造成了'+e_harm+'点伤害，剩余'+p_hp+'点血'
      });
    },1500);
   
      time4 = setTimeout(function(){
      that.setData({
        text:'',
        showBar:true
      })

    },3000);
    
  
  },
  use:function(){
    var that = this;
    wx.request({
      url: 'http://localhost:8080/queryThings',
      method:'GET',
      data:{
        
      },
      header:{
        "content-type":"application/json"
      },
      success:function(res){
        that.setData({
          tlist:res.data,
          showUse:true,
          showBar:false
        })
        
      }
    })  

  },
  useThing:function(e){
    this.setData({
      text:''
    });
    var index = e.currentTarget.dataset.index;
    var tlist = this.data.tlist;
    if(tlist[index].name=='旗帜'){
      this.setData({
        text:'你使用了旗帜，敌人这个回合和下个回合无法行动',
        showBar:true,
        showUse:false,
        flag:true
      })

    }
    if(tlist[index].name=='沙包'){
      var maxhp = this.data.maxhp;
   
      var p_harm =  40;
      var e_hp = this.data.enemy.maxhp - p_harm;
      this.data.enemy.maxhp = e_hp;
      var width = e_hp/maxhp*320;
  
      var e_harm = this.data.enemy.atk - this.data.player.def;
      var p_hp = this.data.player.maxhp - e_harm;
      this.data.player.maxhp = p_hp;
      this.setData({
        text:'你使用了沙包，对敌人造成了40点伤害, 敌人剩余'+e_hp+'点血',
        width:width,
        showBar:false,
        showUse:false
      });
      var that = this;
      var time1 = setTimeout(function(){
       
        that.setData({
          text:'敌人使用了'+that.data.enemy.skill+', 对自己造成了'+e_harm+'点伤害，剩余'+p_hp+'点血'
        });
      },1500);
      var time2 = setTimeout(function(){
        that.setData({
          text:'',
          showBar:true
        })
  
      },3000);
    if(this.data.enemy.maxhp<=0){
      clearTimeout(time1);
      clearTimeout(time2);
      var money = Math.floor((Math.random()*30)+10);
      app.globalData.player.money = app.globalData.player.money + money;
      console.log(app.globalData.player.money);
        this.setData({
          
          text:'对敌人造成了'+p_harm+'点伤害 \n'+'敌人被打败了！获得了'+money+'钱',
          enemy:{},
          showhp:false,
          showzdDialog:false,
          showZhandou:false,
          showTansuo:true
        }) ;
     
    }
    if(this.data.player.maxhp<=0){
      clearTimeout(time1);
      clearTimeout(time2);
      
     
        this.setData({
          
          text:'你被打败了',
          showhp:false,
          showzdDialog:false,
          showZhandou:false,
          dead:true
        }) 
    }

    }
    if(tlist[index].name=='邪箭'){
      var maxhp = this.data.maxhp;
   
      var p_harm =  100;
      var e_hp = this.data.enemy.maxhp - p_harm;
      this.data.enemy.maxhp = e_hp;
      var width = e_hp/maxhp*320;
  
      var e_harm = this.data.enemy.atk - this.data.player.def;
      var p_hp = this.data.player.maxhp - e_harm;
      this.data.player.maxhp = p_hp;
      this.setData({
        text:'你使用了邪箭，对敌人造成了100点伤害, 敌人剩余'+e_hp+'点血',
        width:width,
        showBar:false,
        showUse:false
      });
      var that = this;
      var time1 = setTimeout(function(){
       
        that.setData({
          text:'敌人使用了'+that.data.enemy.skill+', 对自己造成了'+e_harm+'点伤害，剩余'+p_hp+'点血'
        });
      },1500);
      var time2 = setTimeout(function(){
        that.setData({
          text:'',
          showBar:true
        })
  
      },3000);
    if(this.data.enemy.maxhp<=0){
      clearTimeout(time1);
      clearTimeout(time2);
      var money = Math.floor((Math.random()*30)+10);
      app.globalData.player.money = app.globalData.player.money + money;
      console.log(app.globalData.player.money);
        this.setData({
          
          text:'对敌人造成了'+p_harm+'点伤害 \n'+'敌人被打败了！获得了'+money+'钱',
          enemy:{},
          showhp:false,
          showzdDialog:false,
          showZhandou:false,
          showTansuo:true
        }) ;
     
    }
    if(this.data.player.maxhp<=0){
      clearTimeout(time1);
      clearTimeout(time2);
      
     
        this.setData({
          
          text:'你被打败了',
          showhp:false,
          showzdDialog:false,
          showZhandou:false,
          dead:true
        }) 
    }

    }
    var that = this;
    wx.request({
      url: 'http://localhost:8080/useThing',
      data:{
        thing:tlist[index]
      },
      header:{
        "content-type":"application/json"
      },
      success:function(res){
        console.log(res.data);
      }
    })
    

  },
  back:function(){
    this.setData({
      showUse:false,
      showBar:true
    })

  },
  huiyingdi:function(){
    wx.navigateTo({
      url: '/pages/chengjiao/chengjiao',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})